<?php
/**
 * 初始化系统,填充一些必要的属性
 * @author HX<titanrain@163.com>
 */
class InitController extends Controller{

    public function actionBatchDel(){
        $ids=$_REQUEST['ids'];
        $idarr = explode(',',$ids);
        foreach($idarr as $id){
            BookAR::model()->deleteAll("book_id=?",array($id));
            DownloadAR::model()->deleteAll("fk_book=?",array($id));
            UserBookAR::model()->deleteAll("fk_book=?",array($id));
            CommentAR::model()->deleteAll("fk_book=?",array($id));
        }
        echo 'ok';
    }
	
	public function actionDelBook(){
		$sql = "select book_id from ccs8_book where book_size=0";
		$arr = DBUtil::queryColumn($sql);
		foreach($arr as $one){
            BookAR::model()->deleteAll("book_id=?",array($one));
            DownloadAR::model()->deleteAll("fk_book=?",array($one));
            UserBookAR::model()->deleteAll("fk_book=?",array($one));
            CommentAR::model()->deleteAll("fk_book=?",array($one));
		}
		echo $sql;
	}

	/**
	 * 删除那些不存在的书
	 */
	public function actionDeleteDirtyBook($first = 0,$length = 4000){
		$sql = "select book_path,book_id from ccs8_book limit $first,$length";
		$arr = DBUtil::queryAll($sql);
		foreach($arr as $one){
			$file_path = ltrim($one['book_path'],'/');
			if(!file_exists($file_path)){
				BookAR::model()->deleteAll("book_id=?",array($one['book_id']));
				DownloadAR::model()->deleteAll("fk_book=?",array($one['book_id']));
				UserBookAR::model()->deleteAll("fk_book=?",array($one['book_id']));
				CommentAR::model()->deleteAll("fk_book=?",array($one['book_id']));
			}
		}
		echo $sql;
	}
	
	/**
	 * 这个可以复用
	 */
	public function actionInitClickTimes($first = 0,$length = 4000){
		$sql = "select book_id,click_times from ccs8_book limit $first,$length";
		$arr = DBUtil::queryAll($sql);
		$updateSql = 'update ccs8_book set download_times = :download where book_id = :bid';
		$cmd = Yii::app()->db->createCommand($updateSql);
		foreach($arr as $one){
			$downloadTimes = $one['click_times'] - rand(10,300);
			$downloadTimes = $downloadTimes < 0 ? 0 : $downloadTimes;
			$cmd->bindParam(":download",$downloadTimes,PDO::PARAM_INT);
			$cmd->bindParam(":bid",$one['book_id'],PDO::PARAM_INT);
			$cmd->execute();
		}
		echo 0;
	}
	
	/**
	 * 把提前准备好的数据导入
	 */
	public function actionImportBookSizeAndMd5($first = 0,$length = 4000){
		$sql = "select book_id,book_size,book_md5 from ccs8_book_bak limit $first,$length";
		$arr = DBUtil::queryAll($sql);
		$updateSql = 'update ccs8_book set book_size = :size,book_md5=:bmd5 where book_id = :id';
		$cmd = Yii::app()->db->createCommand($updateSql);
		foreach($arr as $one){
			$cmd->bindParam(":size",$one['book_size'],PDO::PARAM_INT);
			$cmd->bindParam(":bmd5",$one['book_md5'],PDO::PARAM_STR);
			$cmd->bindParam(":id",$one['book_id'],PDO::PARAM_INT);
			$cmd->execute();
		}
		echo 0;
	}
	
	/**
	 * 初始化那些book_size=0的书的book_size
	 */
	public function actionInitBookSize(){
		$sql = "select book_path,book_id from book_bak where book_size=0";
		$arr = DBUtil::queryAll($sql);
		$updateSql = 'update book_bak set book_size = :size where book_id = :id';
		$cmd = Yii::app()->db->createCommand($updateSql);
		foreach($arr as $one){
			$file_path = ltrim($one['book_path'],'/');
			if(file_exists($file_path)){
				$size = filesize($file_path);
				$cmd->bindParam(":size",$size,PDO::PARAM_INT);
				$cmd->bindParam(":id",$one['book_id'],PDO::PARAM_INT);
				$cmd->execute();
			} else {
				BookAR::model()->deleteAll("book_id=?",array($one['book_id']));
				DownloadAR::model()->deleteAll("fk_book=?",array($one['book_id']));
				UserBookAR::model()->deleteAll("fk_book=?",array($one['book_id']));
				CommentAR::model()->deleteAll("fk_book=?",array($one['book_id']));
			}
		}
		echo 0;
	}
	
	/**
	 * 删除那些0字节的书
	 */
	public function actionDeleteBookSizeIsZero(){
		$sql = 'delete from ccs8_book where book_size=0';
		DBUtil::exec($sql);
		echo 0;
	}

	/**
	 * 初始化那些新上传的书的book_md5
	 */
	public function actionInitBookMd5(){
		$sql = "select book_path,book_id from book_bak where length(book_md5)<12 and book_size>0";
		$arr = DBUtil::queryAll($sql);
		$updateSql = 'update book_bak set book_md5 = :bmd5 where book_id = :id';
		$cmd = Yii::app()->db->createCommand($updateSql);
		foreach($arr as $one){
			$file_path = ltrim($one['book_path'],'/');
			$bookMd5 = md5_file($file_path);
			$cmd->bindParam(":bmd5",$bookMd5,PDO::PARAM_STR);
			$cmd->bindParam(":id",$one['book_id'],PDO::PARAM_INT);
			try{
				$cmd->execute();
			}catch(Exception $e){
				BookAR::model()->deleteAll("book_id=?",array($one['book_id']));
				DownloadAR::model()->deleteAll("fk_book=?",array($one['book_id']));
				UserBookAR::model()->deleteAll("fk_book=?",array($one['book_id']));
				CommentAR::model()->deleteAll("fk_book=?",array($one['book_id']));
			}
		}
		echo 0;
	}
	
	public function actionUpdateMd5(){
		$sql = 'select book_id,book_size,book_md5 from book_bak order by book_id';
		$data = DBUtil::queryAll($sql);
		$updateSql = 'update ccs8_book set book_md5 = :bmd5,book_size=:size where book_id = :id';
		$cmd = DBUtil::cmd($updateSql);
        $bookBakIds = '';
        $bookIds = '';
		foreach($data as $one){
            $bookBakIds .= $one['book_id'].',';
			$bookId = BookAR::queryByMD5($one['book_md5']);
			if($bookId){
				$bookIds .= $bookId.',';
				continue;
			}
			$cmd->bindParam(":bmd5",$one['book_md5'],PDO::PARAM_STR);
			$cmd->bindParam(":size",$one['book_size'],PDO::PARAM_INT);
			$cmd->bindParam(":id",$one['book_id'],PDO::PARAM_INT);
			$cmd->execute();
		}
        echo "bookBakIds:$bookBakIds<br>bookIds:$bookIds";
	}
	
	/**
	 * 初始化评论数
	 */
	public function actionInitCommentTimes(){
		$sql= "SELECT fk_book,count(comment_id) ccount FROM `ccs8_comment` group by fk_book";
		$arr = DBUtil::queryAll($sql);
		$updateSql = 'update ccs8_book set comment_times = :comments where book_id = :id';
		$cmd = Yii::app()->db->createCommand($updateSql);
		foreach($arr as $one){
			$cmd->bindParam(":comments",$one['ccount'],PDO::PARAM_INT);
			$cmd->bindParam(":id",$one['fk_book'],PDO::PARAM_INT);
			$cmd->execute();
		}
		echo 0;
	}
	
	/**
	 * 初始化上传次数
	 */
	public function actionInitUploadTimes(){
		$sql = 'SELECT fk_user,count(book_id) bcount FROM `ccs8_book` where fk_user<>1 and fk_user<>0 group by fk_user';
		$arr = DBUtil::queryAll($sql);
		$onlyOneUserIdArr = array();
		$muchMoreUploaders = array();
		foreach($arr as $one){
			if($one['bcount'] == 1){
				$onlyOneUserIdArr[$one['fk_user']] = 1;
			}else{
				$muchMoreUploaders[$one['fk_user']] = $one['bcount'];
			}
		}
		$updateSql = 'update ccs8_user set upload_times=1 where user_id in('.join(',',array_keys($onlyOneUserIdArr)).')';
		DBUtil::exec($updateSql);
		$updateSql = 'update ccs8_user set upload_times = :ut where user_id = :uid';
		$cmd = Yii::app()->db->createCommand($updateSql);
		foreach($muchMoreUploaders as $uid=>$uploadTimes){
			$cmd->bindParam(":ut",$uploadTimes,PDO::PARAM_INT);
			$cmd->bindParam(":uid",$uid,PDO::PARAM_INT);
			$cmd->execute();
		}
		echo 0;
	}
	
	/**
	 * 初始化网盘使用大小
	 */
	public function actionInitDiskUsed($first = 0,$length = 3000){
		$sql = "select fk_user,book_size from ccs8_book where fk_user<>1 and fk_user<>0 limit $first,$length";
		$arr = DBUtil::queryAll($sql);
		$updateSql = 'update ccs8_user set disk_size = disk_size + :size,disk_used = disk_used + :used where user_id = :id';
		$cmd = Yii::app()->db->createCommand($updateSql);
		foreach($arr as $one){
			$cmd->bindParam(":size",$one['book_size'],PDO::PARAM_INT);
			$cmd->bindParam(":used",$one['book_size'],PDO::PARAM_INT);
			$cmd->bindParam(":id",$one['fk_user'],PDO::PARAM_INT);
			$cmd->execute();
		}
		echo 0;
	}
}
